#!/usr/bin/env python3.10
# -*- encoding: utf-8 -*-
'''
@File    :   home.py
@Time    :   2023/05/04 16:27:15
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from ..utils.dbhelp import (get_all_post,get_post,
                            get_last_post,get_post_tags,
                            get_tag,get_all_tags,get_tag_posts)

router = APIRouter()


@router.get('/', summary='首页', response_class=HTMLResponse)
async def home(request: Request):
    context = {'request': request,'page':-1}
    return request.app.state.jinja.TemplateResponse("home.html", context=context)

@router.get('/favicon.ico', summary='favicon')
async def favicon():
    return RedirectResponse('/static/favicon.ico')

@router.get("/about", summary='关于我',response_class=HTMLResponse)
async def about(request: Request):
    context = {'request': request}
    return request.app.state.jinja.TemplateResponse("about.html", context=context)


@router.get("/contact", summary='联系我',response_class=HTMLResponse)
async def contact(request: Request):
    context = {'request': request}
    return request.app.state.jinja.TemplateResponse("contact.html", context=context)


@router.get("/posts/{page:int}", response_class=HTMLResponse)
async def posts(request: Request, page: int = 0):
    items_per_page = 10
    post_list = await get_all_post(offset=page * items_per_page, limit=items_per_page)
    context = {'request': request,
               'posts':post_list, 
               'page':page }
    return request.app.state.jinja.TemplateResponse("posts.html", context=context)


@router.get("/lastpost", response_class=HTMLResponse)
async def get_last_postview(request: Request):
    post_obj = await get_last_post()
    tag_list = await get_post_tags(post_obj)
    post_obj.tags = tag_list
    context = {'request': request, 'post': post_obj}
    return request.app.state.jinja.TemplateResponse("post.html", context=context)


@router.get("/post/{pid:int}", response_class=HTMLResponse)
async def get_post_id(request: Request, pid: int):
    post_obj = await get_post(pid)
    tag_list = await get_post_tags(post_obj)
    post_obj.tags = tag_list
    context = {'request': request, 'post': post_obj}
    return request.app.state.jinja.TemplateResponse("post.html", context=context)



@router.get("/tags/{page:int}", response_class=HTMLResponse)
async def get_tags(request: Request,page:int):
    items_per_page = 10
    tags_list = await get_all_tags(offset=page * items_per_page, limit=items_per_page)
    context = {'request': request,
               'tags':tags_list, 
               'page':page }
    return request.app.state.jinja.TemplateResponse("tags.html", context=context)


@router.get("/tag/{tid:int}", response_class=HTMLResponse)
async def get_tag_by_id(request: Request, tid: int):
    tag_obj = await get_tag(tid)
    context = {'request': request, 'tag': tag_obj, 'page':-1}
    return request.app.state.jinja.TemplateResponse("tag.html", context=context)




@router.get("/tag/{tid:int}/{page:int}", response_class=HTMLResponse)
async def get_posts_by_tag(request: Request, tid: int, page = 0):
    items_per_page = 10
    tag_obj = await get_tag(tid)
    posts_list = await get_tag_posts(tag_obj,offset=page * items_per_page, limit=items_per_page)
    context = {'request': request, 'id': tid, 'posts':posts_list,'page':page}
    return request.app.state.jinja.TemplateResponse("tag_posts.html", context=context)


